﻿
#if !(UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_STANDALONE_OSX || STEAMWORKS_WIN || STEAMWORKS_LIN_OSX)
#define DISABLESTEAMWORKS
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class LogsPanel : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public InputField m_Command;
    public Text m_MessageProfab;
    public GameObject m_MessageParent;
    public GameObject m_Panel;
    float m_ShowTimeCount = 0;
    float m_ShowDuration=5;
    bool m_CountSwitch = true;
    void Awake()
    {
        MessageShow.Instance.OnMessageCallback = AddMessage;
        m_Command.onEndEdit.AddListener(OnCommandComplate);
    }
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            OnExpandClicked();
        }
        if(m_Panel.activeSelf)
        {
            if(m_CountSwitch)
                m_ShowTimeCount += Time.deltaTime;
            if(m_ShowTimeCount > m_ShowDuration)
            {
                m_ShowTimeCount = 0;
                m_Panel.SetActive(false);
            }
        }
    }
    public void AddMessage(string value)
    { 
        var profab = GameObject.Instantiate(m_MessageProfab);
        profab.text = value;
        profab.transform.SetParent(m_MessageParent.transform);
        profab.transform.localScale = new Vector3(1,1,1);
        m_Panel.SetActive(true);
    }
    public void OnCommandComplate(string value)
    {
        //if (value == "OpenDevelopmentMode")
        //    Player.Instance.m_DevelopmentMode = true;
        //if (value == "AddMoney")
        //    Player.Instance.Money = 1000000;
        //if (value == "ClearMoney")
        //    Player.Instance.Money = 0;
        //if (value == "MaxDriveLicense")
        //    Player.Instance.m_DriveLevel = Driver.DriveLevel.LEVEL_AP;
        //if (value == "MaxBattleLevel")
        //    Player.Instance.m_BattleLevel = 10;
        //if (value == "BecomeMember")
        //    Commander.Instance.m_Member = 1;

        var values = value.Split();
        if (values.Length == 2 && values[0] == "AddShip")
        {
            int shipTypeID = -1;
            Ship foundShip = null;
            if(int.TryParse(values[1], out shipTypeID))
                foundShip = ShipsManager.Instance.FindPrefabShip(int.Parse(values[1]));
            Player player = PlayerManager.Instance.playerSelf;
            player.AddShip(foundShip);
            if (foundShip == null)
                MessageShow.Instance.Messaage("the id \""+shipTypeID+"\" is invaild");
            else
                MessageShow.Instance.Messaage("you add a ship that be called "+foundShip.m_TypeName+" succeed");
        }

        if(values.Length == 4 && values[0] == "AddItem" && values[2] == "to")
        {
            int itemID = 0, shipIndex = 0;
            if(!int.TryParse(values[1], out itemID) || !int.TryParse(values[3], out shipIndex))
            {
                AddMessage("the commond \""+value + "\" isn't a right command please check the format of the command.");
                return;
            }
            if(PlayerManager.Instance.playerSelf.ships.Count > shipIndex)
            {
                var theSelectedShip = PlayerManager.Instance.playerSelf.ships[shipIndex];
                var addItem = ItemManager.Instance.FindItem(itemID);
                addItem.number = 1;
                theSelectedShip.AddItem(addItem);
            }
        }
        if(value == "OpenDevelopmentMode")
        {
            AddMessage("open development mode succeed!");
            Main.m_GM.m_DevelopmentMode = true;
            return;
        }
        if (value == "CloseDevelopmentMode")
        {
            AddMessage("close development mode succeed!");
            Main.m_GM.m_DevelopmentMode = false;
            return;
        }
        AddMessage(value);
        m_Command.text = "";
    }

    public void OnExpandClicked()
    {

        m_Panel.SetActive(!m_Panel.activeSelf);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        m_CountSwitch = false;
    }

    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
    {
        m_CountSwitch = true;
    }

    private void OnDestroy()
    {
        MessageShow.Instance.OnMessageCallback = null;
    }
}
